package personal.hn.newjava.controller;

import personal.hn.newjava.common.base.BaseController;
import personal.hn.newjava.common.constant.UserConstants;
import personal.hn.newjava.common.result.RestResult;
import personal.hn.newjava.common.result.TableDataInfo;
import personal.hn.newjava.model.vo.SysUserVO;
import personal.hn.newjava.service.SysRoleService;
import personal.hn.newjava.service.SysUserService;
import personal.hn.newjava.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用户表 控制层
 */
@RestController
@RequestMapping("/system/user")
public class SysUserController extends BaseController {
    @Autowired
    SysUserService userService;
    @Autowired
    SysRoleService roleService;

    /**
     * 显示用户表所有数据
     */
    @GetMapping("/list")
    public TableDataInfo list(SysUserVO user, Pageable page) {
        int pageNum = page.getPageNumber() - 1;
        pageNum = pageNum <= 0 ? 0 : pageNum;
        page = PageRequest.of(pageNum, page.getPageSize());
        return userService.selectUserList(user, page);
    }
    //删除用户
    @DeleteMapping("/{userIds}")
    public RestResult remove(@PathVariable Long[] userIds) {
        return toAjax(userService.deleteUserByIds(userIds));
    }
    //新增用户,@RequestBody表示新增用户数据从请求体提交
    @PostMapping
    public RestResult add(@RequestBody SysUserVO user) {
        //唯一性检查，账号，电话，email
        if(UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(user.getUserName()))){
            return RestResult.error("新增用户"+user.getUserName()+"失败，账号已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user))) {
            return RestResult.error("新增用户"+user.getUserName()+"失败，手机号已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(userService.checkEmailUnique(user))){
            return RestResult.error("新增用户"+user.getUserName()+"失败，email已存在");
        }

        user.setCreateBy("system");
        boolean flag=userService.insertUser(user);
        return toAjax(flag ? 1 : 0);
    }
    //修改用户
    @PutMapping
    public RestResult update(@Validated @RequestBody SysUserVO user) {
        //唯一性检查，账号，电话，email
        if (UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user))) {
            return RestResult.error("新增用户"+user.getUserName()+"失败，手机号已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(userService.checkEmailUnique(user))){
            return RestResult.error("新增用户"+user.getUserName()+"失败，email已存在");
        }

        user.setUpdateBy("system");
        boolean flag=userService.updateUser(user);
        return toAjax(flag ? 1 : 0);
    }
    //根据用户id获取详细信息
    @GetMapping(value = {"/","/{userId}"})
    public RestResult getInfo(@PathVariable(value = "userId", required = false) Long userId) {
        RestResult ajax=RestResult.success();
        ajax.put("roles",roleService.selectRoleAll());
        if(StringUtils.isNotNull(userId)){
            ajax.put(RestResult.DATA_TAG,userService.selectUserById(userId));
            ajax.put("roleIds",roleService.selectRoleListByUserId(userId));
        }
        return ajax;
    }
}